//data grid对象
function DataGrid(id) {
	this.id = id;
	this.containerObj = document.getElementById(id + ".container");
	
	this.columns = [];
	this.primaryKey = null;
	
	this.tableObj = null;
	this.tHeadObj = null;
	this.tHeadRowObj = null;
	this.tBodyObj = null;
	
	var oThis = this;
	ContextMenu.intializeContextMenu();
}

DataGrid.prototype.defineColumn = function (key, text, type, width) {
	var columnObj = null;
	if (type == "select") {
		columnObj = new SelectDataColumn(key, text, width);
	} else if (type == "date") {
		columnObj = new DateTimeDataColumn(key, text, width);
	} else if (type == "label") {
		columnObj = new LabelDataColumn(key, text, width);
	} else {
		columnObj = new DataColumn(key, text, width);
	}
		
	this.addColumn(columnObj);
};

DataGrid.prototype.addColumn = function (columnObj) {
	columnObj.gridObj = this;
	columnObj.build();
	this.columns[this.columns.length] = columnObj;
};

DataGrid.prototype.build = function () {
	this.tableObj = document.createElement("table");
	this.tableObj.border = 0;
	this.tableObj.cellPadding = 0;
	this.tableObj.cellSpacing = 0;
	this.tableObj.className = "datagrid";
	this.tableObj.onselectstart = function () {
		return false;
	}
	
	
	this.tHeadObj = document.createElement("thead");
	this.tBodyObj = document.createElement("tbody");
	
	this.tableObj.appendChild(this.tHeadObj);
	this.tableObj.appendChild(this.tBodyObj);
	
	this.buildHead();
	
	this.containerObj.appendChild(this.tableObj);
};

DataGrid.prototype.buildHead = function () {
	this.tHeadRowObj = document.createElement("tr");
	this.tHeadObj.appendChild(this.tHeadRowObj);
	
	var thObj = document.createElement("th");
	thObj.innerText = " ";
	thObj.className = "columnhead";
	this.tHeadRowObj.appendChild(thObj);
	var oThis = this;
	for (var i = 0; i < this.columns.length; i ++) {
		var thObj = document.createElement("th");
		thObj.innerText = this.columns[i].text;
		thObj.width = this.columns[i].width;
		
		thObj.oncontextmenu = function () {
			oThis.showColumnMenu(this);
		}
		this.tHeadRowObj.appendChild(thObj);
	}
	
};

DataGrid.prototype.buildBody = function (ret) {
	if (typeof this.onBodyBuild == "function") {
		this.onBodyBuild(ret);
	}
	for (var i = 0; i < ret.length; i ++) {
		var trObj = document.createElement("tr");
		trObj.setAttribute("primaryKey", ret[i][this.primaryKey]);
		this.tBodyObj.appendChild(trObj);
		this.buildRow(trObj, ret[i]);
	}
};

DataGrid.prototype.clearBody = function () {
	for (var i = 0; this.tBodyObj.rows.length; i ++)
		this.tBodyObj.deleteRow(0);
}

DataGrid.prototype.buildRow = function (trObj, row) {
	var tdObj = document.createElement("td");
	if (row)
		tdObj.innerText = " ";
	else
		tdObj.innerText = "*";
	tdObj.className = "columnhead";
	trObj.appendChild(tdObj);
	
	var oThis = this;
	tdObj.onclick = function () {
		oThis.setRowSelected(this);
	};
	
	tdObj.onblur = function () {
		oThis.clearRowSelected(this);
	};
	
	tdObj.oncontextmenu = function () {
		oThis.setRowSelected(this);
		oThis.showRowMenu(this);
	}
	
	var oThis = this;
	for (var i = 0; i < this.columns.length; i ++) {
		var tdObj = document.createElement("td");
		if (row) tdObj.innerText = row[this.columns[i].key];
		trObj.appendChild(tdObj);
		
		tdObj.onclick = function () {
			this.className = "selected";
			if (typeof oThis.onCellSelected == "function") {
				oThis.onCellSelected(this);
			}
		};
		tdObj.onblur = function () {
			this.className = "";
		};
		tdObj.ondblclick = function () {
			oThis.columns[this.cellIndex-1].show(this);
		}
	}
};

DataGrid.prototype.setRowSelected = function (obj) {
	this.clearRowSelected();
	obj.parentNode.className = 'selected'
	if (typeof this.onRowSelected == "function") {
		this.onRowSelected(obj);
	}
};

DataGrid.prototype.clearRowSelected = function () {
	for (var i = 0 ; i < this.tBodyObj.rows.length ; i ++ ) {
		this.tBodyObj.rows[i].className = "";
	}
};

DataGrid.prototype.showRowMenu = function (tdObj) {
	var oThis = this;
	var popupoptions = [
		new ContextItem("插入", function() { oThis.insertRow(tdObj.parentNode) }, false),
		new ContextItem("添加", function() { oThis.appendRow() }, false),
		new ContextItem("提交", function() { oThis.commitRow(tdObj.parentNode) }, false),
   		new ContextSeperator(),
		new ContextItem("删除", function() { oThis.deleteRow(tdObj.parentNode) }, false),
		new ContextItem("剪切", function() { }, false),
		new ContextSeperator(),
		new ContextItem("上移", function() { }, false),
		new ContextItem("下移", function() { }, false)
	];
	ContextMenu.display(popupoptions);
};

DataGrid.prototype.showColumnMenu = function (thObj) {
	var oThis = this;
	var popupoptions = [
		new ContextItem("升序", function() { }, false),
		new ContextItem("降序", function() { }, false),
   		new ContextSeperator(),
		new ContextItem("左移", function() { }, false),
		new ContextItem("右移", function() { }, false)
	];
	ContextMenu.display(popupoptions);
};


DataGrid.prototype.deleteRow = function (trObj) {
	var pk = trObj.getAttribute("primaryKey");
	if (pk != null) {
		if (this.deleteData(pk)) {
			this.tBodyObj.deleteRow(trObj.sectionRowIndex);
		}
	} else {
		this.tBodyObj.deleteRow(trObj.sectionRowIndex);
	}
};

DataGrid.prototype.appendRow = function () {
	this.insertRow(null);
};

DataGrid.prototype.insertRow = function (trObj) {
	var index = (trObj!=null)?trObj.sectionRowIndex:-1;
	var newTrObj = this.tBodyObj.insertRow(index);
	this.buildRow(newTrObj);
};

DataGrid.prototype.commitRow = function (trObj) {
	var row = {};
	for (var i = 0; i < this.columns.length; i ++) {
		row[this.columns[i].key] = trObj.cells[i+1].innerText;
	}
	
	var pk = this.addData(row)
	if (isValid(pk)) {
		trObj.setAttribute("primaryKey", pk);
		trObj.cells[0].innerText = " ";
	}
};

DataGrid.prototype.fetchData = function (url, method, data) {	
	var http = new HttpObject();
	http.isAsync = true;
		
	var oThis = this;
	http.handleXmlObjects = function (retObjects) {
		oThis.clearBody();
		oThis.buildBody(retObjects);
	};
	
	if (method == 'POST')
		http.httpPost(url, data);
	else
		http.httpGet(url);
};

DataGrid.prototype.updateData = function (pk, updateKey, updateValue) {
	alert(this.primaryKey+"="+pk+"&"+updateKey+"="+updateValue);
	return true;
//	var http = new HttpObject();
//	var ret = null;
//	if (this.method == 'POST')
//		ret = http.httpPostReturnString(this.listURL, "");
//	else
//		ret = http.httpGetReturnString(this.listURL);
//	return ret;
};

DataGrid.prototype.addData = function (row) {
	alert(hashToString(row));
	return "5";
};

DataGrid.prototype.deleteData = function (pk) {
	alert(this.primaryKey+"="+pk);
	return true;
};




/**
 * 使用input输入的column
 */function DataColumn(key, text, width) {
	this.key = key;
	this.text = text;
	this.width = (width?width:"100");
	
	this.columnDiv = document.createElement("div");
	this.columnDiv.className = "datagrid_div";
	this.columnDiv.style.display = "none";
	
	this.columnObj = null;
	this.columnVar = null;
	this.gridObj = null;
}

DataColumn.prototype.build = function () {		
	this.columnObj = document.createElement("input");
	this.columnObj.className = "input";
	this.columnDiv.appendChild(this.columnObj);
	document.body.insertAdjacentElement("beforeEnd", this.columnDiv);
	
	var oThis = this;
	this.columnObj.onblur = function () {
		oThis.hide();
	}
};

DataColumn.prototype.buildColumnObj = function (tdObj) {
	
	this.columnObj.focus();
	this.columnObj.value = tdObj.innerText;
	
	var oThis = this;
	this.columnObj.onkeydown = function () {
		if (event.keyCode == 13) {
			var updateKey = oThis.key;
			var updateValue = this.value;
			var pk = tdObj.parentNode.getAttribute("primaryKey");
			
			if (pk != null) {
				if (oThis.gridObj.updateData(pk, updateKey, updateValue)) {
					tdObj.innerText = this.value;
				}
			} else {
				tdObj.innerText = this.value;
			}
			oThis.hide();
		} else if (event.keyCode == 27) {
			oThis.hide();
		}
	}
};

DataColumn.prototype.show = function (tdObj) {
	var left = posLib.getBoundingLeft(tdObj);
	var top = posLib.getBoundingTop(tdObj);
		
	this.columnDiv.style.left = left-3;
	this.columnDiv.style.top = top-4;
	this.columnDiv.style.display = "block";

	this.buildColumnObj(tdObj);
};

DataColumn.prototype.hide = function () {
	this.columnDiv.style.display = "none";
};




/**
 * 使用select输入的column
 */
function SelectDataColumn (key, text, width) {
	DataColumn.call(this, key, text, width);
}
SelectDataColumn.prototype = new DataColumn;

SelectDataColumn.prototype.build = function () {
	this.columnObj = document.createElement("select");
	this.columnObj.setAttribute("id", "datagrid.column.select." + this.key);
	this.columnObj.className = "select";
	this.columnDiv.appendChild(this.columnObj);
	document.body.insertAdjacentElement("beforeEnd", this.columnDiv);
	
	this.columnVar = new Select("datagrid.column.select." + this.key, "");
	this.columnVar.addOption("1", "选项1");
	this.columnVar.addOption("2", "选项2");
	this.columnVar.addOption("3", "选项3");
	
	var oThis = this;
	this.columnObj.onblur = function () {
		oThis.hide();
	}
}

SelectDataColumn.prototype.buildColumnObj = function (tdObj) {
	
	this.columnObj.focus();
	var value = tdObj.getAttribute("value")
	if (value != null)
		this.columnObj.value = value;
	else {
		var optionsObj = this.columnObj.options;
		for (var i = 0; i < optionsObj.length; i ++) {
			if (optionsObj.item(i).text == tdObj.innerText) {
				this.columnObj.value = optionsObj.item(i).value;
				break;
			}
		}
	}
	
	var oThis = this;
	this.columnObj.onchange = function () {
		var updateKey = oThis.key;
		var updateValue = this.value;
		var pk = tdObj.parentNode.getAttribute("primaryKey");
			
		if (pk != null) {
			if (oThis.gridObj.updateData(pk, updateKey, updateValue)) {
				tdObj.setAttribute("value", this.value);
				tdObj.innerText = this.options[this.selectedIndex].text;
			}
		} else {
			tdObj.setAttribute("value", this.value);
			tdObj.innerText = this.options[this.selectedIndex].text;
		}
		oThis.hide();
	}
};


/**
 * 使用date输入的column
 */
function DateTimeDataColumn (key, text, width, format) {
	DataColumn.call(this, key, text, width);
	this.format = format?format:"%y-%M-%d %h:%m:%s";
}
DateTimeDataColumn.prototype = new DataColumn;
DateTimeDataColumn.prototype.build = function () {
	var thisId = "datagrid.column.date." + this.key;
	this.columnObj = document.createElement("input");
	this.columnObj.setAttribute("id", thisId);
	this.columnObj.setAttribute("format", this.format);
	this.columnObj.readOnly = true;
	this.columnObj.className = "date";
	this.columnDiv.appendChild(this.columnObj);
	
	var columnImage = document.createElement("img");
	columnImage.setAttribute("id", thisId+".button");
	columnImage.src = globeWebAppContext + "/brick/image/datetime.gif";
	columnImage.className = "img_button";
	this.columnDiv.appendChild(columnImage);
	
	var columnDateDiv = document.createElement("div");
	columnDateDiv.setAttribute("id", thisId+".div");
	columnDateDiv.className = "datetime_div";
	columnDateDiv.insertAdjacentHTML("afterBegin", "<iframe style=\"position:absolute;z-index:-1;width:100%;height:100%;top:0;left:0;scrolling:no;\" frameborder=\"0\" src=\"about:blank\"></iframe>")
	//this.columnDiv.appendChild(columnDateDiv);
	
	document.body.insertAdjacentElement("beforeEnd", this.columnDiv);
	document.body.insertAdjacentElement("beforeEnd", columnDateDiv);
	//this.columnDiv.style.height = 500;
	
	this.columnVar = new DateTime(thisId);
	
//	var oThis = this;
//	this.columnDiv.onblur = function () {
//		oThis.hide();
//	}
	
	var oThis = this;
	this.columnObj.onkeydown = function () {
		if (event.keyCode == 27 || event.keyCode == 13) {
			oThis.hide();
		}
	}
};

DateTimeDataColumn.prototype.buildColumnObj = function (tdObj) {
	
	this.columnObj.focus();
	this.columnObj.value = tdObj.innerText;
	
	var oThis = this;
	this.columnObj.onchange = function () {
		var updateKey = oThis.key;
		var updateValue = this.value;
		var pk = tdObj.parentNode.getAttribute("primaryKey");
			
		if (pk != null) {
			if (oThis.gridObj.updateData(pk, updateKey, updateValue)) {
				tdObj.innerText = this.value;
			}
		} else {
			tdObj.innerText = this.value;
		}
		oThis.hide();
	}
};


/**
 * 静态的label column
 */
function LabelDataColumn (key, text, width, format) {
	DataColumn.call(this, key, text, width);
}
LabelDataColumn.prototype = new DataColumn;
LabelDataColumn.prototype.build = function () {};
LabelDataColumn.prototype.show = function () {};
LabelDataColumn.prototype.hide = function () {};
